using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.IO;

namespace Emlyn.Common.Tools.BusinessLayer
{
    public class BasePresentation
    {
        protected XmlElement AddSpan(XmlDocument aDoc, XmlElement aElem, string aClassName, string aValue)
        {
            return AddTag(aDoc, "span", aElem, aClassName, aValue);
        }

        protected XmlElement AddDiv(XmlDocument aDoc, XmlElement aElem, string aClassName, string aValue)
        {
            return AddTag(aDoc, "div", aElem, aClassName, aValue);
        }

        protected XmlElement AddTag(XmlDocument aDoc, string aTagName, XmlElement aElem, string aClassName, string aValue)
        {
            XmlElement retval = aDoc.CreateElement(aTagName);

            XmlAttribute lattrib = aDoc.CreateAttribute("class");
            lattrib.Value = aClassName;
            retval.Attributes.Append(lattrib);
            aElem.AppendChild(retval);

            if (aValue != null)
                retval.InnerText = aValue;

            return retval;
        }

        protected XmlElement AddHref(XmlDocument aDoc, XmlElement aElem, string aUrl, string aDescription)
        {
            XmlElement retval = null;

            if (!String.IsNullOrEmpty(aUrl))
            {
                retval = aDoc.CreateElement("a");

                retval.InnerText = aDescription;
                
                XmlAttribute lattrib = aDoc.CreateAttribute("href");

                lattrib.Value = aUrl;

                retval.Attributes.Append(lattrib);

                aElem.AppendChild(retval);
            }

            return retval;
        }

        protected string GetString(XmlNode aNode)
        {
            string retval = null;

            using (StringWriter ltextWriter = new StringWriter())
            {
                XmlWriterSettings lxmlWriterSettings = new XmlWriterSettings();
                lxmlWriterSettings.CheckCharacters = false;
                lxmlWriterSettings.Indent = true;
                lxmlWriterSettings.ConformanceLevel = ConformanceLevel.Auto;

                using (XmlWriter lxmlWriter = XmlWriter.Create(ltextWriter, lxmlWriterSettings))
                {
                    aNode.WriteContentTo(lxmlWriter);

                    lxmlWriter.Flush();

                    retval = ltextWriter.ToString();
                }
            }

            return retval;
        }
    }
}
